package com.tony.fileserver.service.impl;

import com.tony.fileserver.dao.FileDO;
import com.tony.fileserver.model.FileRepository;
import com.tony.fileserver.service.FileServerService;
import com.tony.fileserver.transferobject.FileDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 普通文件业务逻辑层实现
 */
@Service("fileService")
public class FileServerServiceImpl implements FileServerService {
    @Autowired
    FileRepository fileRepository;

    @Override
    public FileDTO saveFile(FileDTO file) {
        FileDO fileDo = convertFileDtoToFileDo(file);
        FileDO destFile = fileRepository.save(fileDo);

        return convertFileDoToFileDto(destFile);
    }

    @Override
    public List<FileDTO> saveFiles(List<FileDTO> files) {
        List<FileDO> fileDOS = convertDtoToDoList(files);
        List<FileDO> destFiles = fileRepository.save(fileDOS);
        
        return convertDoToDtoList(destFiles);
    }

    private List<FileDO> convertDtoToDoList(List<FileDTO> files) {
        List<FileDO> fileDOS = new ArrayList<>();

        if(files != null && !files.isEmpty()){
            for(FileDTO file: files) {
                fileDOS.add(convertFileDtoToFileDo(file));
            }
        }

        return fileDOS;
    }

    private FileDO convertFileDtoToFileDo(FileDTO file) {
        return new FileDO(file.getName(), file.getContentType(), file.getSize(), file.getMd5(), file.getContent());
    }

    private FileDTO convertFileDoToFileDto(FileDO destFile) {
        FileDTO result = new FileDTO();
        result.setId(destFile.getId());
        result.setName(destFile.getName());
        result.setContentType(destFile.getContentType());
        result.setSize(destFile.getSize());
        result.setMd5(destFile.getMd5());
        result.setContent(destFile.getContent());
        result.setUploadDate(destFile.getUploadDate());
        return result;
    }

    @Override
    public void removeFile(String id) {
        fileRepository.delete(id);
    }

    @Override
    public FileDTO getFileById(String id) {
        return convertFileDoToFileDto(fileRepository.findOne(id));
    }

    @Override
    public long countFiles() {
        return fileRepository.count();
    }

    @Override
    public List<FileDTO> listFiles(int pageIndex, int pageSize) {
        return listFiles(pageIndex, pageSize, true);
    }

    private List<FileDTO> convertDoToDtoList(List<FileDO> files) {
        List<FileDTO> fileDTOS = new ArrayList<>();
        if (files != null && !files.isEmpty()) {
            for (FileDO file : files) {
                fileDTOS.add(convertFileDoToFileDto(file));
            }
        }
        return fileDTOS;
    }

    @Override
    public List<FileDTO> listFiles(int pageIndex, int pageSize, boolean desc) {
        Sort sort = new Sort(desc ? Sort.Direction.DESC : Sort.Direction.ASC, "uploadDate");
        Pageable pageable = new PageRequest(pageIndex, pageSize, sort);
        Page<FileDO> pageFiles = fileRepository.findAll(pageable);
        List<FileDO> files = pageFiles.getContent();

        return convertDoToDtoList(files);
    }
}
